<template>
	<view class="wrap">
		<u-notice-bar mode="horizontal" :list="noticeList"></u-notice-bar>
				<u-search 
				class="u-p-t-20 u-p-b-20" 
				:clearabled="true" 
				placeholder="搜索课件资源" 
				v-model="keyword"
				search-icon-color="#fa3534"
				@search="searchGoods" 
				@custom="searchGoods" 
				@clear="clearSearch"
				bg-color="#ffffff"></u-search>
		

			<u-row gutter="16" class="u-skeleton">
				<u-col span="6" v-for="book in list">
					<view class="books-item" @click="toFile(book.id)" >
						
						<u-image class="u-skeleton-fillet" width="100%" height="300rpx" :src="book.img ? vuex_user.store + book.img : null"></u-image>
						<view class="title u-line-2 u-skeleton-rect">{{book.name ? book.name : '商品名称'}}</view>
						<view class="demo-tag">
					
							<view class="demo-tag-text">
								包含{{book.fileCountFilter}}个课件
							</view>
						</view>
					</view>
				</u-col>
			
			</u-row>
		
		<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus"></u-loadmore>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				noticeList:['建议资源文件在电脑端下载'],
				keyword:'',
				value1: 1,
				value2: 2,
				loadStatus: 'loadmore',
				flowList: [],
				list: [],
				currentPageNum:1,
				totalPage:0
			}
		},
		onLoad() {
			this.getList()
		},
		onReachBottom() {
			if(this.loadStatus == 'nomore') return
			this.loadStatus = 'loading';
			// 模拟数据加载
			setTimeout(() => {
				if(this.currentPageNum < this.totalPage){
					this.currentPageNum++
					this.getList();
					this.loadStatus = 'loadmore';
				}else{
					this.loadStatus = 'nomore';
				}
				
				
			}, 1000)
		},
		methods: {
			toFile(val){
				if(!this.$u.utils.isLogin()){
					this.$u.toast("查看资源请先登录")
					return}// 如果没有登录先去登录
				this.$u.route({
					url:'/pages/pptres/pptShow',
					params: {
						id:val
					}
				})
			},
			async getList(){
				const params = {
					pageNum:this.currentPageNum
				}
				if(!this.$u.test.isEmpty(this.keyword)){
					params.keyword = this.keyword
				}
				console.log(params)
				const res = await this.$u.api.getFileWithBookList(params)
				console.log(res)
				for (var i = 0; i < res.list.length; i++) {
					for (var j = 0; j < res.list[i].bookFiles.length; j++) {
						var arr=res.list[i].bookFiles.filter(function (item, index, array) {
						  if (item.type == 0) {
							return true;
						  }
						})
					}
					res.list[i].fileCountFilter = arr.length
					
				}
				this.list.push(...res.list)
				if(res.list.length < 10){
					this.loadStatus = 'nomore'
				}
				
				this.totalPage = res.totalPage
			},
			clearSearch(){
				this.keyword = ''
				this.clear()
				this.getList()
				
			},
			searchGoods(){
				this.clear()
				this.getList()
			},
			clear(){
				this.loadStatus = 'loading'
				this.currentPageNum = 1,
				this.list = []
			},

		}
	}
</script>

<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		background-color: rgb(240, 240, 240);
	}
</style>

<style lang="scss" scoped>
	.books-item{
		padding: 40rpx;
		margin-top: 30rpx;
		box-shadow: 0 12rpx 20rpx 0 rgba(0,0,0, .1);
		.title{
			margin: 10rpx 0;
			font-weight: 500;
			font-size: 32rpx;
			width: 100%;
		}
		.price{
			color: #B50E03;
			width: 40%;
		}
		.sales{
			color:#888;
			width: 45%;
			font-size: 10rpx;
			
		}
	}
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}
	
	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}
	
	.demo-image {
		width: 100%;
		border-radius: 4px;
	}
	
	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}
	
	.demo-tag {
		display: flex;
		margin-top: 5px;
	}
	
	
	.demo-tag-text {
		margin: 30rpx auto;
		border: 1px solid $u-type-primary;
		color: #FFFFFF;
		background-color: $u-type-primary;
		
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
		
	}
	
	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}
	
	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>